package com.moge.anjian.view;

import android.content.Context;
import android.graphics.PixelFormat;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.widget.TextView;

import com.moge.anjian.beans.Constant;
import com.moge.anjian.R;
import com.moge.anjian.beans.AutoTask;
import com.moge.anjian.utils.ScreenUtils;

public class TargetView extends TextView {

    Context mContext;
    public int index;
    WindowManager.LayoutParams params;
    WindowManager manager;

    AutoTask autoTask = new AutoTask();
    int[] postionArray = new int[2];

    float initX, initY;
    float initTouchX, initTouchY;

    public TargetView(Context context, int index) {
        super(context);
        this.mContext = context;
        this.index = index;
        initView();
    }

    private void initView() {
        params = new WindowManager.LayoutParams(
                ScreenUtils.dp2px(Constant.TARGET_VIEW_WIDTH),
                ScreenUtils.dp2px(Constant.TARGET_VIEW_HEIGHT),
                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT
        );
        this.setBackground(this.getResources().getDrawable(R.drawable.ic_target_arrow_yellow));
        this.setText(String.valueOf(index + 1));
        setTextSize(TypedValue.COMPLEX_UNIT_SP, Constant.TARGET_VIEW_TEXTSIZE);
        setGravity(Gravity.TOP | Gravity.LEFT);

    }

    public void addWindow() {
        manager = (WindowManager) this.getContext().getSystemService(Context.WINDOW_SERVICE);
        manager.addView(this, params);
        this.getLocationOnScreen(postionArray);
        autoTask.x = postionArray[0] + ScreenUtils.dp2px(Constant.TARGET_VIEW_WIDTH) / 2;
        autoTask.y = postionArray[1] + ScreenUtils.dp2px(Constant.TARGET_VIEW_WIDTH) / 2;
    }

    /**
     * 点击穿透
     *
     * @param isPenetrate
     */
    public void setTapPenetrate(boolean isPenetrate) {
        if (params == null) {
            params = (WindowManager.LayoutParams) this.getLayoutParams();
        }
        if (isPenetrate) {
            params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
        } else {
            params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        }
        manager.updateViewLayout(this, params);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
//        Log.e("dimoge", "靶子 onTouchEvent=" + event.getRawX() + ", " + event.getRawY());
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                initX = params.x;
                initY = params.y;
                initTouchX = event.getRawX();
                initTouchY = event.getRawY();
                return true;
            case MotionEvent.ACTION_MOVE:
                params.x = (int) (initX + (event.getRawX() - initTouchX));
                params.y = (int) (initY + (event.getRawY() - initTouchY));
                manager.updateViewLayout(this, params);
                return true;
            case MotionEvent.ACTION_UP:
                int[] postionArray = new int[2];
                this.getLocationOnScreen(postionArray);
                autoTask.x = postionArray[0] + ScreenUtils.dp2px(Constant.TARGET_VIEW_WIDTH) / 2;
                autoTask.y = postionArray[1] + ScreenUtils.dp2px(Constant.TARGET_VIEW_WIDTH) / 2;
            default:
                return false;
        }
    }
}
